'use strict';

define(['controllers', 'codemirror', 'codemirror-xml', 'ztree', '../service/fileService'], function (controllers, CodeMirror) {
    controllers.collection.register('FileController', ['$scope', '$routeParams', 'FileService', function ($scope, $routeParams, fileService) {
        $scope.collectionId = $routeParams.id;

        $scope.init = function () {
            $scope.editMode = false;
            $scope.save_loading = false;
            $scope.selectedNodeData = '';

            var setZKNodeMap = function (zkNodes, zkNodeMap) {
                if (zkNodes != null && zkNodes.length > 0) {
                    for (var i = 0; i < zkNodes.length; i++) {
                        var zkNode = zkNodes[i];
                        zkNodeMap[zkNode.id] = zkNode;
                        setZKNodeMap(zkNode.children, zkNodeMap);
                    }
                }
            };
            var setTreeNodes = function (zkNodes, zkNodeMap, treeNodes) {
                if (zkNodes != null && zkNodes.length > 0) {
                    for (var i = 0; i < zkNodes.length; i++) {
                        var zkNode = zkNodes[i];
                        var treeNode = {
                            id: zkNode.id,
                            pId: zkNode.parentId,
                            name: zkNode.relativePath
                        };
                        if (zkNode.parentId == 0) {
                            treeNode.open = true;
                        }
                        treeNodes.push(treeNode);
                        setTreeNodes(zkNode.children, zkNodeMap, treeNodes);
                    }
                }
            };

            fileService.query({id: $scope.collectionId}, function (zkNodes) {
                for (var i = 0; i < zkNodes.length; i++) {
                    zkNodes[i].parentId = 0;
                }
                $scope.zkNodeMap = {};
                $scope.treeNodes = [];
                setZKNodeMap(zkNodes, $scope.zkNodeMap);
                setTreeNodes(zkNodes, $scope.zkNodeMap, $scope.treeNodes);
                $scope.loadZkTree();
            });

            setTimeout(function () {
                $scope.fileEditor = CodeMirror.fromTextArea(document.getElementById("fileEditor"), {
                    mode: "application/xml",
                    styleActiveLine: true,
                    lineNumbers: true,
                    lineWrapping: true,
                    readOnly: true
                });
            }, 10);
        };

        $scope.loadZkTree = function () {
            var setting = {
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: function (e, treeId, treeNode) {
                        $scope.selectedNode = treeNode;
                        fileService.get({id: $scope.collectionId, nodeId: treeNode.id}, function (node) {
                            $scope.selectedNodeData = node.data == null ? '' : node.data;
                            $scope.fileEditor.setValue($scope.selectedNodeData);
                        });
                    }
                }
            };
            $.fn.zTree.init($("#zkTree"), setting, $scope.treeNodes);
        };

        $scope.edit = function () {
            $scope.editMode = true;
            $scope.fileEditor.setOption('readOnly', false);
        };

        $scope.cancel = function () {
            $scope.editMode = false;
            $scope.fileEditor.setOption('readOnly', true);
            $scope.fileEditor.setValue($scope.selectedNodeData);
        };

        $scope.save = function () {
            if ($scope.selectedNode == null) {
                alert('请选择ZK节点');
                return;
            }
            $scope.save_loading = true;
            var value = $scope.fileEditor.getValue();
            fileService.save({
                id: $scope.collectionId,
                nodeId: $scope.selectedNode.id
            }, {
                data: value
            }, function () {
                $scope.selectedNodeData = value;
                $scope.cancel();
                $scope.save_loading = false;
            });
        };

        $scope.init();
    }]);
});